package com.shangguan.controller;

import com.shangguan.beans.TUser;
import com.shangguan.realm.ShiroRealm;
import com.shangguan.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: LoginController
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 03 月 19 20:26
 * @Version 1.0
 */
@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(ShiroRealm.class)   ;
    @PostMapping(value = "/doLogin",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object dologin(@RequestBody TUser  tUser){
          logger.info("------------"+tUser) ;
        AjaxResult  result = new AjaxResult();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token  =
                new UsernamePasswordToken(tUser.getUserName(),tUser.getUserPassword());
        try{
            subject.login(token);
            result.setSuccess(true);
            result.setMessage("登录成功!");
        } catch (UnknownAccountException e){
             result.setSuccess(false);
             result.setMessage("用户名错误!");
        }catch (IncorrectCredentialsException e){
            result.setSuccess(false);
            result.setMessage("密码错误!");
        } catch (LockedAccountException e){
            result.setSuccess(false);
            result.setMessage("账户被冻结!");
        } catch (AuthenticationException e){
            result.setSuccess(false);
            result.setMessage("用户信息错误!");
        }
        return result;
    }
}
